<?php
/*
+-----------------------------------------------------------------------------+
| $Id: channel.php 2010-05-24 12:37:44Z Bleakwind $
| Content detail handle
| Copyright (c) 2003-2010 Bleakwind (www.weaverdream.com)
| http://www.weaverdream.com/
| Release under the GNU Lesser General Public License Version 3 (LGPLv3):
|   http://www.gnu.org/licenses/lgpl.html
+-----------------------------------------------------------------------------+
*/

if (!defined( 'ENTRY_INDEX')){
    echo "<h1>Forbidden</h1><p>You don't have permission to access on this server.</p>";
    exit;
}

if ($sys->get['ope'] == "process") {
    // update_click
    function content_process()
    {
        global $LANGUAGE,$LANGLIST,$SETTING,$CONFIGURE,$MEMBER,$ADMIN,$db,$sys,$c,$ac;
        $ajax_response = new xajaxResponse();
        if( preg_match("/^[1-9][0-9]{0,10}$/",$sys->get['scheduler_id']) ){
            $scheduler_info = $sys->return_scheduler_info($sys->get['scheduler_id']);
            if (preg_match("/^[1-9][0-9]*$/",$scheduler_info['scheduler_id']) &&
                preg_match("/^[1-9][0-9]*$/",$scheduler_info['channel_id']) &&
                preg_match("/^[1-9][0-9]*$/",$scheduler_info['content_id'])) {
                $sql_data = array(
                    "click" => "click+1",
                );
                $result = func::db_update(DB_TABLE_CONTENT, $sql_data, "content_id='".$scheduler_info['content_id']."'");
                if (!$result) {
                    $ajax_response->alert($LANGUAGE['s']['content']['update_click_error']."<!-- update_click_error -->");
                } else {
                    $content_info = func::db_select(DB_TABLE_CONTENT, "if_turn,if_turn_url", "content_id=".$scheduler_info['content_id']);
                    $content_info = $content_info[0];
                    if ($content_info['if_turn'] == "1" && !empty($content_info['if_turn_url'])){
                        $ajax_response->redirect($content_info['if_turn_url']);
                    }
                }
            }
        }
        return $ajax_response;
    }
    $bwajax->register(XAJAX_FUNCTION, "content_process");

    $new_template = "page_blank.htm";

} else {

    $bwajax->configure('requestURI', $CONFIGURE['common']['control_index']."?act=content&ope=process&scheduler_id=".$sys->get['scheduler_id']);
    $bwajax->register(XAJAX_FUNCTION, "content_process");

    if( !preg_match("/^[1-9][0-9]{0,10}$/",$sys->get['scheduler_id']) ){
        $sys->prompt("failed",$LANGUAGE['s']['content']['scheduler_id_error']."<!-- scheduler_id_error -->");
    } else {

        $scheduler_info = $sys->return_scheduler_info($sys->get['scheduler_id']);

        if (!preg_match("/^[1-9][0-9]*$/",$scheduler_info['scheduler_id'])) {
            $sys->prompt("failed",$LANGUAGE['s']['content']['scheduler_id_not_exist']."<!-- scheduler_id_not_exist -->");
        } elseif (!preg_match("/^[1-9][0-9]*$/",$scheduler_info['channel_id'])) {
            $sys->prompt("failed",$LANGUAGE['s']['content']['channel_id_not_exist']."<!-- channel_id_not_exist -->");
        } elseif (!preg_match("/^[1-9][0-9]*$/",$scheduler_info['content_id'])) {
            $sys->prompt("failed",$LANGUAGE['s']['content']['content_id_not_exist']."<!-- content_id_not_exist -->");
        } else {

            $sys->return_channel_info($scheduler_info['channel_id']);

            $sys->return_setmodel_info($sys->channel_info['setmodel_id']);
            $sys->return_table_list();
            $sys->return_field_array();
            $sys->return_model_array();

            $content_info   = $sys->return_content_info($scheduler_info['scheduler_id'],false);

            if ($content_info['content']['if_turn'] == "1") {
                $new_template = "page_redirect.htm";
            } else {

                $array_select   = array("content_prefix","content_color","content_fontstyle","content_decoration");
                $select_option  = $sys->return_select($array_select);

                $trans_list     = $sys->return_trans_list($sys->channel_info['setmodel_id']);

                // Template for setmodel
                if (!empty($sys->setmodel_info['template_content']) && file_exists($t->template_dir.$sys->setmodel_info['template_content'])) {
                    $new_template = $sys->setmodel_info['template_content'];
                    if (substr($new_template, -4, 4) == ".htm") {
                        $dir_css_new    = substr($new_template, 0, -4)."/css/css.css";
                        $dir_js_new     = substr($new_template, 0, -4)."/js/js.js";
                    } elseif (substr($new_template, -5, 5) == ".html") {
                        $dir_css_new    = substr($new_template, 0, -5)."/css/css.css";
                        $dir_js_new     = substr($new_template, 0, -5)."/js/js.js";
                    }
                }

                // Template for setmodel
                if (!empty($sys->channel_info['template_content']) && file_exists($t->template_dir.$sys->channel_info['template_content'])) {
                    $new_template = $sys->channel_info['template_content'];
                    if (substr($new_template, -4, 4) == ".htm") {
                        $dir_css_new    = substr($new_template, 0, -4)."/css/css.css";
                        $dir_js_new     = substr($new_template, 0, -4)."/js/js.js";
                    } elseif (substr($new_template, -5, 5) == ".html") {
                        $dir_css_new    = substr($new_template, 0, -5)."/css/css.css";
                        $dir_js_new     = substr($new_template, 0, -5)."/js/js.js";
                    }
                }

                // Template for setmodel
                if (!empty($content_info['content']['template_content']) && file_exists($t->template_dir.$content_info['content']['template_content'])) {
                    $new_template = $content_info['content']['template_content'];
                    if (substr($new_template, -4, 4) == ".htm") {
                        $dir_css_new    = substr($new_template, 0, -4)."/css/css.css";
                        $dir_js_new     = substr($new_template, 0, -4)."/js/js.js";
                    } elseif (substr($new_template, -5, 5) == ".html") {
                        $dir_css_new    = substr($new_template, 0, -5)."/css/css.css";
                        $dir_js_new     = substr($new_template, 0, -5)."/js/js.js";
                    }
                }

                if (preg_match("/^[1-9][0-9]{0,10}$/i", $sys->channel_info['id'])) {
                    $sys->return_channel_path($sys->channel_info['id']);
                }

                $t->assign(array(
                    "channel_info"          => $sys->channel_info,
                    "channel_path"          => $sys->channel_path,

                    "setmodel_info"         => $sys->setmodel_info,
                    "table_list"            => $sys->table_list,
                    "field_array"           => $sys->field_array,
                    "model_array"           => $sys->model_array,

                    "scheduler_info"        => $scheduler_info,
                    "content_info"          => $content_info,

                    "select_option"         => $select_option,
                    "trans_list"            => $trans_list,
                ));
            }
        }
    }
}
if (!empty($sys->channel_path)) { 
    foreach($sys->channel_path as $v){ 
        $public_var['page_place'][] = $v['name']; 
        $public_var['page_keyword'] = array_merge((array)$public_var['page_keyword'], array_filter(preg_split("/[\s,]+/", $v['keyword'])));
    }
}
?>
